# ---------------------------------------------------------
# TUM - Technichal University of Munich
#
# Original Authors: Taylor Jones
#                   Magdalena Altmann
# Further Authors:  Adrian
#                   Friedrich Klappenbach
# Date: 2020-12-06
# Purpose: This code is only to start the inversion in from
#          the campaign  folder with the according 
#          parameters.
# 
# Usage:
#     Campaign Folder that contains:
#       observations sub folder (\obs)
#       inventories sub folder (\inventories)
#       footprints sub folder (\foot)
#       Run_Inversion.R (this File)
#     execute Run_Inversion.R after adjusting parameters accordingly
# ---------------------------------------------------------

rm(list = ls(all.names = TRUE))  # clear R environment (clear workspace)

# this is the path to the inverse modeling engine including \\ at the end. 
path_inverse_modeling_engine <- paste0(dirname(rstudioapi::getSourceEditorContext()$path),"/../inverse_modeling_engine/")
# path_inverse_modeling_engine <- "C:/Path/to/inverse_modeling_engine/"

# shapes of administrative regions:
path_geojson<-"gemeinden_simplify200.geojson"

# Campaign prefix:
prefix <- 'MUC'

# Select dates that should be evaluated: e.g. 20180827 for Aug. 27th in 2018
# multiple dates possible: dates <- c(20180818, 20180820)
dates <- c(20180827)

# browser() ## a break point inserted here

# Select station names that should be evaluated from folder "obs\XXYYMMDD.csv"
# XX denotes prefix (e.g. MUC for munich)
ems     <- c('mb','ma','ki','kj','wa')

#where the ems were: 
#ToDo: Fetch data automatically
em_lats <- c(48.261,48.204,48.151,48.086,48.023)  # latitudes of em27 locations
em_lons <- c(11.671,11.880,11.569,11.279,11.716)  # longitudes of em27 locations


# Select footprint data file: The prefix will be added
# . e.g. "_ERA5_bw8_small"
foot_suffix <- "_ERA5_bw8_small.nc"

############### INVENTORIES ######################

sectors <- c("total")
inventory_file  <- 'CH4_bigger.nc'
coordslat_file <- 'coord_lat.nc'
coordslon_file <- 'coord_lon.nc'



#Constants and Conversions:
to_ggyr <- 16.04*365.25*60*60*24*1e-9  #umol/m2s -> Gg/yr ...still need to multiply by total domain area in km2!

# using preprocessed data: apply_scaling = FALSE; using observations directly: TRUE
apply_scaling = TRUE  # what does this do??
# solve inversion for all measurement days together: invert_all_days = TRUE
invert_all_days = FALSE  # also produces a nicer bar plot


#Parameters for Inversion:  -------------------------------------------------------------------------------------------------------------
sigma_sector_priors <- list("total"=1e2)
sigma_obs_prior     <- 2e-4      # ppm  (0.2 ppb)
sigma_bkgd_prior    <- 1e-3      # ppm  (10 ppb) 1e-2 
t_back              <- 3*60*60   # seconds: The timescale (e-folding rate) of background variability
bkgd_prior          <- 1.85      # ppm: prior background value

#Parameters for Bootstrap: --------------------------------------------------------------------------------------------------------------
sector_to_bootstrap <- "total"
bs_x_err            <- 2e-4  # ppm:  (0.2 ppb) error with which to draw for bootstrap
bs_y_err            <- 2e-4  # ppm:  (0.2 ppb) error with which to draw for bootstrap
bootstrap_reps      <- 5000  # times to run each of the bootstraps


path_campaign <- dirname(rstudioapi::getSourceEditorContext()$path)

fig_directory <- paste0(path_campaign,"/figs")

if (!dir.exists(fig_directory)){
  dir.create(fig_directory)
  message('fig-folder created.')
}


source(paste0(path_inverse_modeling_engine,'inversion_2018.R'))
